// pages/storeManage/storeManage.js
const skinBehavior = require("../../../utils/themeSkin/skinBehavior")
var app = getApp();
Page({
  behaviors:[skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    formTarget:'',
    name_of_withdrawal:"",
    store_state:0,
    show:false,
    turnover_time:['今日','昨日','全部'],
    turnover_time_index:2,
    language: "",
    store: {
      name: "",
      thumb: "",
    },
    store_product_set: false,
    income: {
      money_total: 0.0,
      sure_withdraw_money: 0.0,
    },

    status:0,
    storeManageOpen:true,
    details:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'-----------options')
    if(options.is_travel_agency){
      this.setData({
        formTarget:options.is_travel_agency
      })
      wx.setNavigationBarTitle({
  　　　　title: '旅行社中心' //页面切换，更换页面标题
  　　})
    } else {
      wx.setNavigationBarTitle({
  　　　　title: '千县千品中心' //页面切换，更换页面标题
  　　})
    }
    this.checkrouter();
    this.getStoreInfo();
    this.showCash();
    this.customizeIncome();
    this.getStoreData()
  },
  getStoreData(){
    let urlStr
    if(this.data.formTarget){
      urlStr = app.getNetAddresss('plugin.travel-agency.frontend.store.center.index.get-statistics')
    } else {
      urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.center.index.get-statistics')
    }
    app._postNetWork({
      url:urlStr,
      data:{day_type:0},
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result){
          this.setData({details:data})
          return
        }
        app.tips(msg);
      }
    })
  },
  store_popup(){
    this.setData({
      show:true,
      store_state:this.data.store.operating_state
    })
  },
  onChange(event){
    this.setData({
      store_state: event.detail,
    });
  },
   //自定义提现收入语言
   customizeIncome() {
    try {
      var value = wx.getStorageSync('mailLanguage');
      if (value) {
        let mailLanguage = JSON.parse(value);
        // Do something with return value
        this.setData({
          name_of_withdrawal:mailLanguage.income.name_of_withdrawal
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  onClose(){
    this.setData({show:false})
  },
  turnover_handle(e){
    let index = e.currentTarget.dataset.index;
    this.setData({turnover_time_index:index});
    let day_type=0;
    if(index==0){
      day_type=1;
    }else if(index==1){
      day_type = 2
    }
    let urlStr
    if(this.data.formTarget){
      urlStr = app.getNetAddresss('plugin.travel-agency.frontend.store.center.index.get-statistics')
    } else {
      urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.center.index.get-statistics')
    }
    app._postNetWork({
      url:urlStr,
      data:{day_type},
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result){
          this.setData({details:data})
          return
        }
        app.tips(msg);
      }
    })
  },
  checkrouter() {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let basic_info = wx.getStorageSync("yz_basic_info");
      let stop_info = basic_info.popularize_page.mini.vue_route;
      for (let i = 0; i < stop_info.length; i++) {
        if (stop_info[i] == "storeManage") {
          this.setData({
            storeManageOpen:false
          });
          if(!basic_info.popularize_page.mini.mini_url){
            wx.showModal({
              content: '未开启推广权限',
              showCancel:false,
              success:function(res){
                if(res.confirm){
                  wx.navigateBack({
                    delta: 1
                  });
                }
              }
            });
          }else{
            wx.showToast({
              title: "未开启推广权限",
              duration: 1000,
              icon: "none",
              success: function () {
                setTimeout(() => {
                  wx.redirectTo({
                    url: basic_info.popularize_page.mini.mini_url,
                  });
                }, 1000);
              },
            });
          }
          console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");
          return;
        }
      }
    } catch (e) {
      console.log(e);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  getCash(){
    wx.navigateTo({
      url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=store&isCash=1',
    });
  },
  // 显示续费按钮
  showCash() {
    let urlStr
    if(this.data.formTarget){
      urlStr = app.getNetAddresss(
        "plugin.travel-agency.frontend.store.enter.enable"
      );
    } else {
      urlStr = app.getNetAddresss(
        "plugin.store-cashier.frontend.store.enter.enable"
      );
    }
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if(res.result !=1 ) return app.tips(res.msg);
        this.setData({
          status:res.data.status
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onShareTimeline(){},
  goStoreCode(){
    wx.navigateTo({
      url: "/packageC/o2o/qrCode/qrCode?store_id=" + this.data.store.store_id+'&key=store',
    });
  },
  goqrCode() {
    wx.navigateTo({
      url: "/packageC/o2o/qrCode/qrCode?store_id=" + this.data.store.store_id,
    });
  },
  gostoreOrderlist() {
    wx.navigateTo({
      url:
        "/packageA/member/myOrder_v2/myOrder_v2?status=0" + "&orderType=store",
    });
  },
  gopresentationRecord() {
    if(this.data.formTarget == 1){
      wx.navigateTo({
        url:
          "/packageA/member/presentationRecord_v2/presentationRecord_v2?i=" +
          this.data.store.store_id +
          "&type=travel_agency",
      });
    } else {
      wx.navigateTo({
        url:
          "/packageA/member/presentationRecord_v2/presentationRecord_v2?i=" +
          this.data.store.store_id +
          "&type=store",
      });
    }
  },
  goCommodityManagement() {
    wx.navigateTo({
      url:
        "/packageA/member/supplier/CommodityManagement/CommodityManagement?store_id=" +
        this.data.store.store_id +
        "&store=store&formtarget="+this.data.formTarget,
    });
  },
  goGoodsClassify() {
    wx.navigateTo({
      url:
        "/packageC/o2o/GoodsClassify/GoodsClassify?store_id=" +
        this.data.store.store_id,
    });
    // 'GoodsClassify',{store_id: store.store_id}
  },
  gotocloudStock(){
    wx.navigateTo({
      url: '/packageI/store_cloud_inventory/index/index'
    })
  },
  getStoreInfo() {
    let that = this;
    let urlStr
    if(this.data.formTarget){
      urlStr = app.getNetAddresss(
        "plugin.travel-agency.frontend.store.center.index.index"
      );
    } else {
      urlStr = app.getNetAddresss(
        "plugin.store-cashier.frontend.store.center.index.index"
      );
    }
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            store: res.data.store,
            income: res.data.income,
            nearby: res.data.nearby,
            specify_show: res.data.specify_show,
            is_open_code:res.data.is_open_code,
            my_store_order:res.data.my_store_order
          });
          if (res.data.store_product_set == true) {
            that.setData({
              store_product_set: res.data.store_product_set,
            });
          }
          if(res.data.show_store_balance_award){
            that.setData({
              show_store_balance_award:res.data.show_store_balance_award
            });
          }
        } else {
          if (res.data.url && res.data.url.search('/o2o/storeApply') >= 0) {
            wx.redirectTo({
              url: '/packageA/member/storeApply/storeApply',
            });
            return;
          }
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  gowithdrawal() {
    wx.navigateTo({
      url: "/packageA/member/withdrawal/withdrawal",
    });
  },
  // 跳转到优惠券管理
  goDiscountsGoods() {
    wx.navigateTo({
      url:
        "/packageB/member/commodity_coupon/commodity_coupon/commodity_coupon",
    });
  },
  // 跳转我的千县千品
  gomyStore() {
    if (this.data.specify_show == "1") {
      wx.navigateTo({
        url:
          "/packageC/o2o/o2oStore/o2oStore?store_id=" +
          this.data.store.store_id +
          "&fromHome=1",
      });
    } else {
      wx.navigateTo({
        url:
          "/packageC/o2o/HomeSeller/HomeSeller?store_id=" +
          this.data.store.store_id,
      });
    }
  },
  gotoStoreGroup(e) {
    let key = e.currentTarget.dataset.key;
    if(key == 'storeBalance'){
      wx.navigateTo({
        url:'/packageA/member/presentationRecord_v2/presentationRecord_v2?orderType='+key,
      });
    }else{
      wx.navigateTo({
        url:
          "/packageC/o2o/o2oGroupActivitylist/o2oGroupActivitylist?store_id=" +
          this.data.store.store_id,
      });
    }
  },
  //跳转到千县千品附近推荐商品
  gorecommendlist() {
    wx.navigateTo({
      url:
        "/packageD/o2o/recommendind/recommendind?store_id=" +
        this.data.store.store_id,
    });
  },
  //跳转导入商品
  goAddGoods() {
    wx.navigateTo({
      url:
        "/packageC/o2o/store_add_goods/store_add_goods?store_id=" +
        this.data.store.store_id +
        "&store=store",
    });
  },

  //修改店铺状态
  modifyStoreStatus() {
    let urlStr
    if(this.data.formTarget){
      urlStr = app.getNetAddresss(
        "plugin.travel-agency.frontend.store.center.index.changeStatus"
      );
    } else {
      urlStr = app.getNetAddresss(
        "plugin.store-cashier.frontend.store.center.index.changeStatus"
      );
    }
    app._postNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        wx.showToast({
          title:res.msg,
          icon: "none",
          duration: 1000,
        });
        if(res.result == 1){
          this.setData({show:false});
          this.getStoreInfo();
        }
      }
    });
  },
});
